package com.geekgo.flowcontroller.core.acquire;

import com.geekgo.flowcontroller.core.converter.IRateConverter;
import com.google.common.util.concurrent.RateLimiter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @author geekgo
 * @version 2016/8/4 18:54
 */
public class DPSLimitAcquirable extends RateLimitAcquirable {

    private static final Logger LOGGER = LogManager.getLogger(DPSLimitAcquirable.class);

    private IRateConverter converter;

    public DPSLimitAcquirable(RateLimiter rateLimiter,IRateConverter converter) {
        super(rateLimiter);
        this.converter = converter;
    }

    public IRateConverter getConverter() {
        return converter;
    }

    @Override
    public void acquire(Object[] args) {
        int acquire = getConverter().acquire(args);
        LOGGER.debug("acquire for {}",acquire);
        getRateLimiter().acquire(acquire);
    }
}
